﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AgileSaude.Data.Repository
{
    public class tipoServicoRepository : ItiposervicoRepository
    {
        agilesaudeEntities entity;

        public tipoServicoRepository()
        {
            entity = agilesaudeContext.getInstance();
        }

        public IQueryable<tiposervico> Gettiposervicos()
        {
            return agilesaudeContext.getInstance().tiposervico;
        }
        
        public IList<tiposervico> GetAll()
        {
            return agilesaudeContext.getInstance().tiposervico.ToList();
        }

        public tiposervico gettiposervico(int ptiposervicoId)
        {
            return entity.tiposervico.SingleOrDefault(a => a.idTipoServico == ptiposervicoId);
        }

        public void Delete(tiposervico ptiposervico)
        {
            try
            {
                entity.tiposervico.DeleteObject(ptiposervico);
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                agilesaudeContext.removeInstance();
                throw ex;
            }
        }

        public void Add(tiposervico ptiposervico)
        {
            try
            {
                entity.AddTotiposervico(ptiposervico);
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                agilesaudeContext.removeInstance();
                throw ex;
            }
        }

        public void Update()
        {
            try
            {
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                agilesaudeContext.removeInstance();
                throw ex;
            }
        }

        public tiposervico gettiposervico(string pDescr)
        {
            return entity.tiposervico.SingleOrDefault(a => a.Descricao == pDescr);
        }
    }
}
